package com.fr.privilege.providers.dao;

import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.base.XMLable;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableData;
import com.fr.data.impl.NameTableData;
import com.fr.privilege.authentication.Authentication;
import com.fr.privilege.authority.Authority;
import com.fr.privilege.providers.AbstractAuthenticationProvider;
import com.fr.privilege.user.User;
import com.fr.privilege.user.UserDetails;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.web.core.JSONReader;
import com.fr.web.platform.PlatformUtils;
import java.util.ArrayList;
import java.util.List;

public class DaoAuthenticationProvider extends AbstractAuthenticationProvider
{
  private static final long serialVersionUID = -2564032298178563645L;
  private UserConfig userConfig;
  private RoleConfig roleConfig;

  public UserConfig getUserConfig()
  {
    return this.userConfig;
  }

  public void setUserConfig(UserConfig paramUserConfig)
  {
    this.userConfig = paramUserConfig;
  }

  public RoleConfig getRoleConfig()
  {
    return this.roleConfig;
  }

  public void setRoleConfig(RoleConfig paramRoleConfig)
  {
    this.roleConfig = paramRoleConfig;
  }

  public boolean authenticate(Authentication paramAuthentication)
    throws Exception
  {
    if (paramAuthentication == null)
      throw new Exception(Inter.getLocText("Privilege-The_Authentication_Can_Not_Be_Null") + ".");
    if (paramAuthentication.isAuthenticated())
      return true;
    String str1 = paramAuthentication.getPrincipal().toString();
    String str2 = paramAuthentication.getCredentials().toString();
    User localUser = loadUserByUsername(str1);
    if (localUser == null)
      throw new Exception(Inter.getLocText("Privilege-Invalid_User_Or_Password") + ".");
    if (!(ComparatorUtils.equals(localUser.getPassword(), str2)))
      throw new Exception(Inter.getLocText("Privilege-Invalid_User_Or_Password") + ".");
    paramAuthentication.setAuthenticated(true);
    paramAuthentication.setAuthorities(localUser.getAuthorities());
    return true;
  }

  public void readJSON(JSONObject paramJSONObject)
    throws JSONException
  {
    super.readJSON(paramJSONObject);
    UserConfig localUserConfig = new UserConfig();
    localUserConfig.readJSON(paramJSONObject);
    setUserConfig(localUserConfig);
    RoleConfig localRoleConfig = new RoleConfig();
    localRoleConfig.readJSON(paramJSONObject);
    setRoleConfig(localRoleConfig);
  }

  public Authority[] listAuthorities()
    throws Exception
  {
    if (this.roleConfig == null)
      throw new Exception("The role config can not be null.");
    if (StringUtils.isBlank(this.roleConfig.getTableDataName()))
      throw new Exception("The tabledata can not be null.");
    NameTableData localNameTableData = new NameTableData(this.roleConfig.getTableDataName());
    int[] arrayOfInt = PlatformUtils.getColumnIndics(localNameTableData, new String[] { this.roleConfig.getRoleField() });
    if (arrayOfInt[0] < 0)
      return null;
    int i = localNameTableData.getRowCount();
    ArrayList localArrayList = new ArrayList();
    for (int j = 0; j < i; ++j)
    {
      String str = localNameTableData.getValueAt(j, arrayOfInt[0]).toString();
      Authority localAuthority = new Authority(str);
      if (!(localArrayList.contains(localAuthority)))
        localArrayList.add(localAuthority);
    }
    return ((Authority[])localArrayList.toArray(new Authority[localArrayList.size()]));
  }

  private User loadUserByUsername(String paramString)
    throws Exception
  {
    if (this.userConfig == null)
      throw new Exception(Inter.getLocText("Privilege-The_User_Config_Can_Not_Be_Null") + ".");
    if (this.roleConfig == null)
      throw new Exception(Inter.getLocText("Privilege-The_Role_Config_Can_Not_Be_Null") + ".");
    try
    {
      String str3;
      String str1 = this.userConfig.getTableDataName();
      if (StringUtils.isBlank(str1))
        return null;
      NameTableData localNameTableData1 = new NameTableData(str1);
      int[] arrayOfInt1 = PlatformUtils.getColumnIndics(localNameTableData1, new String[] { this.userConfig.getUsernameField(), this.userConfig.getPasswordField() });
      String str2 = null;
      int i = 0;
      int j = localNameTableData1.getRowCount();
      for (int k = 0; k < j; ++k)
      {
        str3 = localNameTableData1.getValueAt(k, arrayOfInt1[0]).toString();
        if (str3.equals(paramString))
        {
          i = 1;
          str2 = localNameTableData1.getValueAt(k, arrayOfInt1[1]).toString();
          break;
        }
      }
      Authority[] arrayOfAuthority = null;
      if (i != 0)
      {
        NameTableData localNameTableData2;
        int[] arrayOfInt2;
        int i1;
        str3 = this.roleConfig.getTableDataName();
        ArrayList localArrayList = new ArrayList();
        if (StringUtils.isNotBlank(str3))
        {
          localNameTableData2 = new NameTableData(str3);
          arrayOfInt2 = PlatformUtils.getColumnIndics(localNameTableData2, new String[] { this.roleConfig.getUsernameField(), this.roleConfig.getRoleField() });
          int l = localNameTableData2.getRowCount();
          for (i1 = 0; i1 < l; ++i1)
          {
            String str4 = localNameTableData2.getValueAt(i1, arrayOfInt2[0]).toString();
            if (str4.equals(paramString))
            {
              String str5 = localNameTableData2.getValueAt(i1, arrayOfInt2[1]).toString();
              localArrayList.add(new Authority(str5));
            }
          }
        }
        arrayOfAuthority = (Authority[])localArrayList.toArray(new Authority[localArrayList.size()]);
      }
      return ((i != 0) ? new UserDetails(paramString, str2, arrayOfAuthority) : null);
    }
    catch (Exception localException)
    {
      throw new Exception(Inter.getLocText("Database") + Inter.getLocText("Datasource-Connection_failed") + "!");
    }
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      if ("UserConfig".equals(str))
        this.userConfig = ((UserConfig)ReportXMLUtils.readXMLable(paramXMLableReader));
      else if ("RoleConfig".equals(str))
        this.roleConfig = ((RoleConfig)ReportXMLUtils.readXMLable(paramXMLableReader));
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (this.userConfig != null)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, this.userConfig, "UserConfig");
    if (this.roleConfig != null)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, this.roleConfig, "RoleConfig");
  }

  public static class RoleConfig
    implements JSONReader, XMLable
  {
    public static final String XML_TAG = "RoleConfig";
    private String tableDataName;
    private String usernameField;
    private String roleField;

    public String getTableDataName()
    {
      return this.tableDataName;
    }

    public void setTableDataName(String paramString)
    {
      this.tableDataName = paramString;
    }

    public String getUsernameField()
    {
      return this.usernameField;
    }

    public void setUsernameField(String paramString)
    {
      this.usernameField = paramString;
    }

    public String getRoleField()
    {
      return this.roleField;
    }

    public void setRoleField(String paramString)
    {
      this.roleField = paramString;
    }

    public void readXML(XMLableReader paramXMLableReader)
    {
      if (paramXMLableReader.isChildNode())
      {
        String str = paramXMLableReader.getTagName();
        if (str.equals("RoleConfigAttr"))
        {
          this.tableDataName = paramXMLableReader.getAttr("tabledata");
          this.usernameField = paramXMLableReader.getAttr("username");
          this.roleField = paramXMLableReader.getAttr("role");
        }
      }
    }

    public void writeXML(XMLPrintWriter paramXMLPrintWriter)
    {
      paramXMLPrintWriter.startTAG("RoleConfigAttr");
      if (StringUtils.isNotBlank(this.tableDataName))
        paramXMLPrintWriter.attr("tabledata", this.tableDataName);
      if (StringUtils.isNotBlank(this.usernameField))
        paramXMLPrintWriter.attr("username", this.usernameField);
      if (StringUtils.isNotBlank(this.roleField))
        paramXMLPrintWriter.attr("role", this.roleField);
      paramXMLPrintWriter.end();
    }

    public void readJSON(JSONObject paramJSONObject)
      throws JSONException
    {
      if (paramJSONObject.has("ur_tabledata"))
        this.tableDataName = paramJSONObject.getString("ur_tabledata");
      if (paramJSONObject.has("ur_role"))
        this.roleField = paramJSONObject.getString("ur_role");
      if (paramJSONObject.has("ur_user"))
        this.usernameField = paramJSONObject.getString("ur_user");
    }

    public Object clone()
      throws CloneNotSupportedException
    {
      return super.clone();
    }
  }

  public static class UserConfig
    implements JSONReader, XMLable
  {
    public static final String XML_TAG = "UserConfig";
    private String tableDataName;
    private String usernameField;
    private String passwordField;

    public String getTableDataName()
    {
      return this.tableDataName;
    }

    public void setTableDataName(String paramString)
    {
      this.tableDataName = paramString;
    }

    public String getUsernameField()
    {
      return this.usernameField;
    }

    public void setUsernameField(String paramString)
    {
      this.usernameField = paramString;
    }

    public String getPasswordField()
    {
      return this.passwordField;
    }

    public void setPasswordField(String paramString)
    {
      this.passwordField = paramString;
    }

    public void readXML(XMLableReader paramXMLableReader)
    {
      if (paramXMLableReader.isChildNode())
      {
        String str = paramXMLableReader.getTagName();
        if (str.equals("UserConfigAttr"))
        {
          this.tableDataName = paramXMLableReader.getAttr("tabledata");
          this.usernameField = paramXMLableReader.getAttr("username");
          this.passwordField = paramXMLableReader.getAttr("password");
        }
      }
    }

    public void writeXML(XMLPrintWriter paramXMLPrintWriter)
    {
      paramXMLPrintWriter.startTAG("UserConfigAttr");
      if (StringUtils.isNotBlank(this.tableDataName))
        paramXMLPrintWriter.attr("tabledata", this.tableDataName);
      if (StringUtils.isNotBlank(this.usernameField))
        paramXMLPrintWriter.attr("username", this.usernameField);
      if (StringUtils.isNotBlank(this.passwordField))
        paramXMLPrintWriter.attr("password", this.passwordField);
      paramXMLPrintWriter.end();
    }

    public void readJSON(JSONObject paramJSONObject)
      throws JSONException
    {
      this.tableDataName = paramJSONObject.getString("up_tabledata");
      this.usernameField = paramJSONObject.getString("up_user");
      this.passwordField = paramJSONObject.getString("up_password");
    }

    public Object clone()
      throws CloneNotSupportedException
    {
      return super.clone();
    }
  }
}